package com.nightsoul.dubbo.provider.service.impl;


import com.alibaba.dubbo.config.annotation.Service;
import com.alibaba.dubbo.rpc.RpcContext;
import com.nightsoul.dubbo.bean.Person;
import com.nightsoul.dubbo.provider.service.CacheService;
import com.nightsoul.dubbo.provider.service.DemoService;

@Service()
public class DemoServiceImpl implements DemoService{
	//@Reference只会查找其它服务提供者注册的服务，而不能直接引用自己本身注册的服务
	//@Reference	//因为需要依赖其它服务提供者，为其它注册方便将其注释
	private CacheService cacheService;
	
	@Override
	public String sayHello(String name) {
		return "hello " + name;
	}

	@Override
	public void testContext() {
		boolean isProviderSide = RpcContext.getContext().isProviderSide();
		String clientIp = RpcContext.getContext().getRemoteHost();
		// 获取当前服务配置信息，所有配置信息都将转换为URL的参数
		String application = RpcContext.getContext().getUrl().getParameter("application");
		System.out.println(String.format("provider side: %b, client ip: %s, application: %s", isProviderSide, clientIp, application));
		
		// 注意：每发起RPC调用，上下文状态会变化
		cacheService.findCache("张三");
		// 此时本端变成消费端，这里会返回false
		System.out.println("provider side: " + RpcContext.getContext().isProviderSide());		
	}

	@Override
	public void testImplicitParameter() {
		// 获取客户端隐式传入的参数，用于框架集成，不建议常规业务使用
		String index = RpcContext.getContext().getAttachment("index");
		System.out.println("idnex=" + index);
	}

	@Override
	public Person getPerson(Integer id) {
		return new Person(id, "charles`son", 4);
	}

}
